package com.raycloud.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.raycloud.constants.SystemConstant;
import com.raycloud.dao.EndProjectDAO;
import com.raycloud.dao.FrontProjectDAO;
import com.raycloud.dao.VersionDAO;
import com.raycloud.pojo.EndProject;
import com.raycloud.pojo.FrontProject;
import com.raycloud.pojo.Version;
import com.raycloud.query.EndProjectQuery;
import com.raycloud.query.FrontProjectQuery;
import com.raycloud.query.VersionQuery;
import com.raycloud.util.CmdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by donar on 17/4/19.
 */
@Controller
@RequestMapping("/front")
public class FrontProjectController {
    @Autowired
    FrontProjectDAO frontProjectDAO;
    @Autowired
    EndProjectDAO endProjectDAO;
    @Autowired
    VersionDAO versionDAO;
    @RequestMapping(value = "init")
    @ResponseBody
    public Response initProject(@ModelAttribute FrontProject project)  {
        if(StringUtils.isEmpty(project.getProjectName())) return Response.error("项目名不能为空");
        if(frontProjectDAO.getFrontProjectByKey(project.getProjectName())!=null) return Response.error("项目名已存在");
        if(StringUtils.isEmpty(project.getOwner())) return Response.error("责任人不能为空");
        if(StringUtils.isEmpty(project.getGitPath())) return Response.error("git路径不能为空");
        if(StringUtils.isEmpty(project.getBranch())) return Response.error("请选择分支");
        String cmd = "sh /data/front/new.sh {0} {1} {2}";
        String cmd2 = MessageFormat.format(cmd,SystemConstant.FRONT_PATH+"/"+project.getProjectName(),project.getGitPath(),project.getBranch());
        System.out.println(cmd2);
        String result = CmdUtil.exec(cmd2);
        frontProjectDAO.addFrontProject(project);
        return Response.success(result);
    }
    @RequestMapping(value = "projectList")
    @ResponseBody
    public Response getProjectList()  {
        return Response.success(frontProjectDAO.getFrontProjectList(new FrontProjectQuery()));
    }
    @RequestMapping(value = "delete")
    @ResponseBody
    public Response deleteProject(String projectName)  {
        List<Version> versions = versionDAO.getVersionList(new VersionQuery());
        for (Version v:versions){
            String[] frontversions = v.getFrontProjectsInfo().split(";");
            for (String frontv:frontversions){
                if(frontv.equals(projectName))return Response.error("版本号"+v.getVersionId()+"正关联此项目。请先删除相关版本再删除该项目");
            }
        }
        String result = CmdUtil.exec("rm -rf "+ SystemConstant.FRONT_PATH+"/"+projectName);
        frontProjectDAO.deleteByKey(projectName);
        return Response.success("删除成功");
    }
    @RequestMapping(value = "publish")
    @ResponseBody
    public Response restart(String projectName)  {
        String result = CmdUtil.exec("sh "+SystemConstant.FRONT_PATH+"/pull.sh "+projectName);
        return Response.success(result);
    }

}
